package org.incubator.numeric;

import java.util.Random;

import org.incubator.Generator;

/**
 * Generates a random integer within a range of possible values.
 * This generator performs very well, especially with a large
 * set of possibile values.
 * 
 * @author Jeroen van Schagen
 * @since 1-5-2011
 */
public class MinMaxIntegerGenerator implements Generator<Integer> {
    private final Random random = new Random();
    private final IntegerRange range;

    /**
     * Construct a new {@link MinMaxIntegerGenerator}.
     * @param range the range of possible values
     */
    public MinMaxIntegerGenerator(IntegerRange range) {
        this.range = range;
    }

    /**
     * Construct a new {@link MinMaxIntegerGenerator}.
     * @param min starting value of range
     * @param max ending value of range
     */
    public MinMaxIntegerGenerator(int min, int max) {
        this(new IntegerRange(min, max));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer generate() {
        return random.nextInt(range.length()) + range.start();
    }

}
